package com.sc.nft.task;


import com.sc.nft.dao.UserMerchantPledgeTaskRecordsDao;
import com.sc.nft.entity.MerchantPledgeTaskSettlementRewardsLog;
import com.sc.nft.entity.UserMerchantPledgeTaskRecords;
import com.sc.nft.enums.UserMerchantPledgeTaskStatusEnum;
import com.sc.nft.service.MerchantPledgeTaskSettlementRewardsLogService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class MerchantPledgeTask {

    private final MerchantPledgeTaskSettlementRewardsLogService merchantPledgeTaskSettlementRewardsLogService;
    private final UserMerchantPledgeTaskRecordsDao userMerchantPledgeTaskRecordsDao;

    /**
     *  1.每天0点 未开始设置到 开始状态
     *  2.初始化结算周期到 sc_merchant_pledge_task_settlement_rewards_log
     * @throws Exception
     */
    @XxlJob("maintenancePledgeStatus")
    public void maintenancePledgeStatus() throws Exception {
        //设置开启
        List<UserMerchantPledgeTaskRecords> userMerchantPledgeTaskRecords = userMerchantPledgeTaskRecordsDao.getListByStatus(UserMerchantPledgeTaskStatusEnum.NOT_YET_STARTED);
        userMerchantPledgeTaskRecords.forEach(merchantPledgeTaskSettlementRewardsLogService::maintenancePledgeStatus);
    }


    @XxlJob("autoUserMerchantPledgeTaskRecords")
    public void autoUserMerchantPledgeTaskRecords() throws Exception {
        //自动质押
        List<UserMerchantPledgeTaskRecords>  list = merchantPledgeTaskSettlementRewardsLogService.getExpireList();
        list.forEach(merchantPledgeTaskSettlementRewardsLogService::autoUserMerchantPledgeTaskRecords);

    }



    /**
     *  1.sc_merchant_pledge_task_settlement_rewards_log 中获取数据
     *  2.结算周期奖励和收益奖励
     *  3.如果过期了重新插入一条新的记录  Pledge status maintenance
     * @throws Exception
     */
    @XxlJob("pledgeTaskSettlementRewards")
    public void pledgeTaskSettlementRewards() throws Exception {
        List<MerchantPledgeTaskSettlementRewardsLog> list = merchantPledgeTaskSettlementRewardsLogService.getPledgeTaskSettlementRewards();
        list.forEach(merchantPledgeTaskSettlementRewardsLogService::pledgeTaskSettlementRewards);
    }
}
